/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.commands;

import controller.Constants;
import core.ConnectException;
import core.HighLightTracker;
import core.PopupTracker;
import core.Tracker;
import core.TriggerPort;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import model.graphic.objects.GTrigger;
import view.GUI.LabelReader;
import view.GUI.graphic.GraphicView;

/**
 *
 * @author Arfeniel
 */
public class ExecuteThread extends Thread {

    TriggerPort tp = null;
    GTrigger trigger = null;
    private GraphicView gview;

    public ExecuteThread(TriggerPort triggerPort, GTrigger trigger, GraphicView gview) {
        this.tp = triggerPort;
        this.gview = gview;
        this.trigger = trigger;
    }

    @Override
    public void run() {
        try {
            try {
                if (PopupTracker.class.isAssignableFrom(Tracker.getInstance().getClass()) || HighLightTracker.class.isAssignableFrom(Tracker.getInstance().getClass())) {
                    gview.notifyObservers(new RunCommand(false));
                    Thread.sleep(Constants.getHighlighttime());
                    trigger.setHighlighted(true);
                    gview.giveAttention();
                    Thread.sleep(Constants.getHighlighttime());
                    trigger.setHighlighted(false);
                    gview.giveAttention();
                    Thread.sleep(Constants.getHighlighttime());
                    trigger.setHighlighted(true);
                    gview.giveAttention();
                    Thread.sleep(Constants.getHighlighttime());
                    trigger.setHighlighted(false);
                    gview.giveAttention();
                    Thread.sleep(Constants.getHighlighttime());
                    trigger.setHighlighted(true);
                    gview.giveAttention();
                    Thread.sleep(Constants.getHighlighttime());
                    trigger.setHighlighted(false);
                    gview.giveAttention();
                    Thread.sleep(Constants.getPausetime());
                    if (PopupTracker.class.isAssignableFrom(Tracker.getInstance().getClass())) {
                        JOptionPane.showMessageDialog(gview.getParent(), "TriggerPort " + tp + ": " + LabelReader.getInstance().getString("callStarted"));
                    }
                }
                tp.call();
            } catch (InterruptedException ex) {
                Logger.getLogger(HighLightTracker.class.getName()).log(Level.SEVERE, null, ex);
            }

        } catch (ConnectException ex) {
            JOptionPane.showMessageDialog(gview.getParent(), "ExecuteThread: " + ex.getMessage(), LabelReader.getInstance().getString("Error"), JOptionPane.ERROR_MESSAGE);
        }
        gview.notifyObservers(new RunCommand(true));
    }
}
